<template>
  <div class="mine">
    <!-- 已登录 -->
    <div v-if="user" class="header logined">
      <div class="user-info">
        <div class="left">
          <van-image
            round
            fit="cover"
            class="avatar"
            :src="useInfo.photo"
          />
          <!-- https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202105%2F11%2F20210511193549_08e64.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1673704778&t=5b3e76a2fb85488779ca67060e093a98 -->
          <span>{{useInfo.name}}</span>
        </div>
        <div class="right">
          <van-button size="mini" round @click="goEdit">编辑资料</van-button>
        </div>
      </div>
      <div class="data-starts">
        <div class="data-item">
          <span class="count">{{useInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{useInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{useInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{useInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录 -->
    <div v-else class="header no-login">
      <div class="login-btn" @click="goLogin">
        <img class="mobile-img" src="~@/assets/img/mobile.png" alt="登录按钮" />
        <span>登录 / 注册</span>
      </div>
    </div>

    <!-- 导航 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <van-cell title="消息通知" is-link></van-cell>
    <van-cell class="mb-9" title="小智同学" is-link></van-cell>
    <van-cell v-if="user" class="logout-cell" clickable title="退出登录" @click="goBack"></van-cell>

  </div>
</template>

<script>
import { mapState } from 'vuex'
import { userInfo } from '@/api/user'

export default {
  data () {
    return {
      useInfo: {},
      userList: {}
    }
  },
  created () {
    if (this.user) {
      this.getUserInfo()
    }
  },
  methods: {
    // 获取用户信息
    async getUserInfo () {
      try {
        const { data: res } = await userInfo()
        this.useInfo = res.data
      } catch (err) {
        console.log('用户信息获取失败')
      }
    },
    // 点击登录跳转到登录页
    goLogin () {
      this.$router.push('/login')
    },
    // 点击按钮，编辑用户资料
    goEdit () {

    },
    // 点击退出登录 清空 vuex 的 user
    goBack () {
      this.$dialog.confirm({
        title: '退出提示',
        message: '是否确定退出登录?'
      })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(() => {})
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.header {
  height: 361px;
  background: linear-gradient(to top, #a8edea 0%, #fed6e3 100%);
}
.no-login {
  display: flex;
  justify-content: center;
  align-items: center;
  .login-btn {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile-img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    span {
      font-size: 28px;
    }
  }
}
.logined {
  .user-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 231px;
    padding: 76px 32px 23px;
    box-sizing: border-box;
    .left {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .avatar {
        width: 132px;
        height: 132px;
        border: 4px solid #fff;
      }
      span {
        margin-left: 23px;
        font-size: 30px;
        color: #fff;
      }
    }
  }
  .data-starts {
    display: flex;
    .data-item {
      flex: 1;
      height: 130px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      color: #fff;
      .count {
        font-size: 36px;
      }
      .text {
        font-size: 23px;
      }
    }
  }
}
.grid-nav {
  .grid-item {
    height: 141px;
    .iconfont {
      font-size: 45px;
    }
    .icon-shoucang {
        color: #eb5253;
      }
      .icon-lishi {
        color: #ff9d1d;
      }
    span {
      font-size: 28px;
    }
  }
}
.logout-cell {
  margin:20px;
  width: calc(100% - 40px);
  box-sizing: border-box;
  text-align: center;
  color: #e37979;
}
</style>
